// grass/pages/grassTopics/grassTopics.js
var common = require("../../../common/common.js").common;

var common = require("../../../common/common.js").common;

var app = getApp();

const STATE_EMPTY = 0;

const STATE_LOADING = 1;

const STATE_SUCCEED = 2;

Page({
    /**
   * 页面的初始数据
   */
    data: {
        page: 1,
        pageSize: 10,
        showLoading: 1,
        allBottom: 0,
        //2为加载完  1为加载中  0为还有
        pagesLength: 1,
        //仅有一页，不显示返回按钮
        grassList: [],
        colors: [ "#153641", "#22556E", "#4799B7", "#6DB3BF", "#94CFC9" ],
        imgInfo: {},
        colorCount: 5,
        state: STATE_EMPTY
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        let that = this;
        that.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        let pagesLength = getCurrentPages().length;
        var isIPX = app.globalData.isIPX;
        that.setData({
            isIPX: isIPX,
            pagesLength: pagesLength
        });
        wx.getSystemInfo({
            success: ({screenWidth: screenWidth}) => {
                that.screenWidth = screenWidth;
            }
        });
        if (options.info) {
            let info = JSON.parse(options.info);
            that.setData({
                image: info.img,
                topic_num: info.topic_num,
                topic_id: info.id,
                title: info.title,
                info: options.info
            });
        }
        console.log("image" + that.data.image);
        that.getNewGrass();
    },
    toHex(n) {
        n = parseInt(n, 10);
        if (isNaN(n)) return "00";
        n = Math.max(0, Math.min(n, 255));
        return "0123456789ABCDEF".charAt((n - n % 16) / 16) + "0123456789ABCDEF".charAt(n % 16);
    },
    rgbToHex(R, G, B) {
        return this.toHex(R) + this.toHex(G) + this.toHex(B);
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {},
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {
        wx.stopPullDownRefresh();
    },
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {
        let that = this;
        that.getNewGrass();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {
        return {
            title: this.data.title,
            imageUrl: this.data.image,
            path: "/grass/pages/grassTopics/grassTopics?info=" + JSON.stringify(this.data.info)
        };
    },
    //全局基础配置信息
    getBase: function(openid) {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                that.setData({
                    color_header: res.extra.color_header,
                    color_text: res.extra.color_text,
                    color_main: res.extra.color_main,
                    color_minor: res.extra.color_minor,
                    style: res.extra.style
                });
            }
        });
    },
    /**
   * 获取种草
   */
    getNewGrass: function() {
        let that = this;
        this.setData({
            showLoading: 1,
            allBottom: 0
        });
        common.request({
            endpoint: "Plantgrass/list/list",
            method: "post",
            data: {
                topic_id: that.data.topic_id,
                page: that.data.page,
                rows: that.data.pageSize
            },
            success: res => {
                if (res.rows.length < 1) {
                    that.setData({
                        allBottom: 2,
                        showLoading: 0
                    });
                    return;
                }
                let grassList = res.rows;
                grassList.map(x => {
                    if (x.type == 1) {
                        if (!x.img) {
                            x.img = [];
                            x.img.push(that.data.domain + "img/weixin_default.png");
                        } else {
                            x.img = x.img.split(",");
                        }
                    } else {
                        if (!x.cover_img) {
                            x.cover_img = that.data.domain + "img/weixin_default.png";
                        }
                    }
                });
                grassList = that.data.page == 1 ? res.rows : that.data.grassList.concat(res.rows);
                that.setData({
                    grassList: grassList,
                    page: that.data.page + 1,
                    showLoading: 0
                });
            }
        });
    },
    /**
 * 跳转视频或者图文详情页
 */
    toDetail: function(e) {
        let type = e.currentTarget.dataset.item.type;
        if (type == 1) {
            //图文
            wx.navigateTo({
                url: "/grass/pages/grassDetail/grassDetail?id=" + e.currentTarget.dataset.item.id
            });
        } else {
            wx.navigateTo({
                url: "/grass/pages/grassVideo/grassVideo?id=" + e.currentTarget.dataset.item.id
            });
        }
    }
});